package sg.edu.nus.iss.jass.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @version 1.0
 * @author SE19 Team 1
 * 
 */
public class PropertyUtils {
    
    public PropertyUtils(){}
    
    // 1. Reads value based on the value of key
    public static String readValue(String filePath,String key) {
        Properties props = new Properties();
        try {
            InputStream in = new BufferedInputStream (new FileInputStream(filePath));
            props.load(in);
            String value= props.getProperty(key);
            System.out.println("Class:PropertyUtils, Method:readValue, key:"+key+",value:"+value);
            return value;
        }
        catch(Exception ex) {
            ex.printStackTrace();               
            return null;
        }
    }
    
    // 2. Reads an enumeration of keys from a file
    public static  Enumeration getKeys(String filePath) {
        Enumeration en;
        Properties props = new Properties();
        
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            en  = props.propertyNames();
            return en;
        }
        catch(Exception ex) {
        	ex.printStackTrace();
        	return null;
        }
    }
    
    // 3. Get the all the keys and values as a Object Properties
    public static Properties readProperties(String filePath) {
        Properties props = new Properties();
        
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            in.close();
            return props;
        }
        catch(Exception ex) {
            return null;
        }
    }
    
    // 4. Write to specific property file
    public static void writeProperties(String filePath,String parameterKey,String parameterValue) {
        Properties prop = new Properties();
        
        try {
            InputStream fis = new FileInputStream(filePath);
            //read key value pair from file input stream 
            prop.load(fis);
            OutputStream fos = new FileOutputStream(filePath);
            /*
             * Calls the Hashtable method put. Provided for parallelism with the getProperty method. 
             * Enforces use of strings for property keys and values. 
             * The value returned is the result of the Hashtable call to put. 
             * 
             * */
            prop.setProperty(parameterKey, parameterValue);
            /*
             * Writes this property list (key and element pairs) in this Properties table to
             * the output stream in a format suitable for loading into a Properties table 
             * using the load method
             * */
            prop.store(fos, "Last Update, key:"+parameterKey+",value: "+parameterValue);
            fos.close();
            fis.close();
        }
        catch(Exception ex) {
             System.err.println("Visit "+filePath+" for updating "+parameterKey+" value error");
        }
    }
    
    // 5. Remove one record based on the key specified
    public static void removeProperty(String filePath,String key){
        Properties props = new Properties();
        
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath)); 
            props.load(in);
            props.remove(key);
            //create a new file output stream to hold the property file in which 
            //entry has been deleted.
            OutputStream fos =  new FileOutputStream(filePath);
            props.store(fos, "Last Update,"+new Date()+" Deletion: key:"+key);
        }
        catch(Exception ex) {
        	System.out.println("key is null"+ key);
        }
    }
    
    // 6. Update record based on key specified
    public static void updateProperty(String filePath,String key,String value) {
      Properties props = new Properties();
      
      try {
          InputStream in =  new BufferedInputStream(new FileInputStream(filePath));
          props.load(in);
          props.setProperty(key, value);
          OutputStream fos = new FileOutputStream(filePath);
          props.store(fos, "Last Update, key:"+key+",value: "+value);
          fos.close();
          in.close();
      }
      catch(Exception ex) {
          ex.printStackTrace();
      }
    }
    
    public static void updateProperties(String filePath, Map map) {
      Properties props = new Properties();
      
      try {
          InputStream in =  new BufferedInputStream(new FileInputStream(filePath));
          props.load(in);
          props.putAll(map);
          OutputStream fos = new FileOutputStream(filePath);
          props.store(fos, "Last Update at " + new Date());
          fos.close();
          in.close();
      }
      catch(Exception ex) {
          ex.printStackTrace();
      }
    }
    
    public static boolean checkKeyAvailability(String filePath, String key) {
        boolean flag = true; //By Default all keys all available
        Enumeration keys = getKeys(filePath);
        while(keys.hasMoreElements()){
            String keyStr = (String)keys.nextElement();
            if(keyStr.equalsIgnoreCase(key)){
                flag=false;
            }
        }
        return flag;
    }
}
